<?php
include_once "BaseDAO.php";
include_once $_SERVER['DOCUMENT_ROOT'].'/ad/sys/com/model/util/plog/classes/plog.php';

class UserDAO extends BaseDAO {
	
	public function __construct () {
		parent::__construct();
		$this->log = Plog::factory(__FILE__);			
	}
	
	public function modifyUserTiget($user) {
		 $user = (Object)$user;
		$sql = "update user set 
					password='$user->password' ,companyName = '$user->companyName',
					name='$user->name',phone='$user->phone',
					county='$user->county',area='$user->area',zipcode='$user->zipcode',
					address='$user->address',ModifyTime='$user->ModifyTime',Logo='$user->logo'
				where account = '$user->account'";
			$count =  mysql_query($sql);
			$this->log->debug($sql);
			return $count;
	}
	
	public function modifyUserManager($user) {
		 $user = (Object)$user;
		$sql = "update user set 
					account = '$user->account', password='$user->password' ,companyName = '$user->companyName',
					count = '$user->count',	name='$user->name',phone='$user->phone',
					type='$user->type',proxyAccount='$user->proxyAccount',status='$user->status',
					county='$user->county',area='$user->area',zipcode='$user->zipcode',
					address='$user->address',ModifyTime='$user->ModifyTime',Logo='$user->logo'
				where account = '$user->account'";
			$count =  mysql_query($sql);
			return $count;
	}
	
	public function  modifyManagerAccount($account, $password, $modifyAccount) {
		$sql = "update user set account = '$modifyAccount', password='$password' where account = '$account'";
		$count =  mysql_query($sql);
		return $count;
	}
	
	public function modifyUser($account,  $password, $status) {
		$sql  = "update user set account = '$account' ,password ='$password' , status='$status' where account='$account'";		
		$count =  mysql_query($sql);
		return $count;
	}
	
	public function getUserAll($start ,$limit){
		$sqlCount = "select count(account) from user";
		$count =mysql_fetch_row(mysql_query($sqlCount));
		
		$sql = "select * from user order by CreateTime desc limit $start ,$limit";
		$items = $this->toArray(@mysql_query($sql));
		
		return   array( "items" => $items, "count" => $count[0]);
	}
	
	public function updateUserNotify(
					   $account,
					   $notifyBudgetUpdate,
					   $notifyAdApprovedNot,
					   $notifyAdModify,
					   $notifyAdApproved,
					   $notifyPayComplete) {
		   	
		  $sql =  "update user set  
		  			 notifyBudgetUpdate=$notifyBudgetUpdate,
		  			 notifyAdApprovedNot=$notifyAdApprovedNot,
		  			 notifyAdModify=$notifyAdModify,
		  			 notifyAdApproved=$notifyAdApproved,
		  			 notifyPayComplete=$notifyPayComplete 
		  			 where account = '$account'";
		  
		 $this->log->debug("更新使用者設定:".$sql);
		 $result = mysql_query($sql); 			
	 	 $this->log->debug("更新使用者設定結果:".$result);
		 return $result;
	}
	
	public function getManagerUser($account, $password) {
		
	}
	 
	public function getUserByAccount($account, $isStatus = true) {
		$sql = "";
		if ($isStatus) {
			$sql = "select * from user  where  account = '$account'  and status=1";
		} else {
			$sql = "select * from user  where  account = '$account'";
		}

		$result = @mysql_query($sql);
		$row = @mysql_fetch_object($result);
		return $row;
	}
	
	public function getUser($account, $password) {
		$sql = "select * from user  where  account = '$account' and password = '$password' and status=1";
		$result = @mysql_query($sql);
		$row = @mysql_fetch_object($result);
		return $row;
	}
	
	public function getMenagerUser($account, $password) {
		$sql = "select * from user  where  account = '$account' and type=0 and password = '$password' and status=1";
		$result = @mysql_query($sql);
		$row = @mysql_fetch_object($result);
		return $row;
	}
	
	public function getMenagerUserAccount() {
		$sql = "select account from user  where  status=1 and type=0";
		$result = @mysql_query($sql);
		
		while ($row = @mysql_fetch_object($result)) {
			$row_set[] = $row->account;
		} 
		
		return $row_set;
	}
	
	
	public function getSettilDetailByAccount($account) {
		$sql = "select count, 
				(select max(createtime) from settledetail where account = u.account)     createtime
				from user u 
				where account='$account'";
		$result = @mysql_query($sql);
		$row = @mysql_fetch_object($result);
		return $row;
	}
	
	 
	public function queryTargetAccountMember($account , $start , $limit) {
		$countSql = "select count(u.account) from user u where u.proxyAccount ='$account'";
		$countResult = @mysql_query($countSql);
		$count = mysql_fetch_row($countResult);
		
		$sql = "select u.account, u.name, u.status, u.createTime, 
				(select count(s.ChkSettle)  from settledetail s where s.account = u.account and s.ChkSettle=0 limit 1)  isPay,
				(select count(a.IsNew) from ad a where a.account = u.account and a.isNew =0 limit 1 ) isNewAd
				from user u where u.proxyAccount ='$account' limit $start , $limit";
		$result = @mysql_query($sql);
		$items = $this::toArray($result);
		
		return  array(
			"items" => $items,
			"count" => $count[0]
		);
	}
	
	/**
	 * 管者者查詢會員資料
	 */
	public function queryMenagerMemeber($account ,$name, $type, $isPay, $isNew,$start, $limit) {
		
		$accountStr = is_null($account) || $account =="" ? "" : " and u.account like'%$account%'";
		$nameStr    = is_null($name)    || $name=="" ? "" : " and u.name like'%$name%'";
		$typeStr    = is_null($type)    || $type =="" ? "" : " and u.type=$type";
	 
		$isPayStr   = "";
		$isPayCount = "";
		$isNewStr = "";
		$isNewCount = "";
		
	 	
		if ((!is_null($isPay) && $isPay !="") && !is_null($isNew) && $isNew !="") {
		     
			if ($isPay ==1) {
			 	$isPayStr =  " having(count > 0 ";
				$isPayCount = " and (select count(s.count)  from settledetail s where s.account = u.account and s.chkSettle=1)>0";
			} else {
			 	$isPayStr =  " having(count = 0 ";
				$isPayCount = " and (select count(s.count)  from settledetail s where s.account = u.account and s.chkSettle=1)=0";
			}
			if ($isNew == 1) {
				$isNewStr = " and isNew > 0) ";
				$isNewCount = " and (select count(s.count)  from settledetail s where s.account = u.account and s.chkSettle=1)>0 ";
			} else {
				$isNewStr = " and isNew = 0) ";
				$isNewCount = " and (select count(s.count)  from settledetail s where s.account = u.account and s.chkSettle=1)=0 ";
			}
		} else  if (!is_null($isPay) && $isPay !="" ) {
			 if ($isPay ==1) {
			 	$isPayStr = "  having ( count > 0)";
			 	$isPayCount = " and (select count(s.count)  from settledetail s where s.account = u.account and s.chkSettle=1)>0";
			 } else {
			 	$isPayStr = "  having ( count = 0)";
			 	$isPayCount = " and (select count(s.count)  from settledetail s where s.account = u.account and s.chkSettle=1)=0";
			 }
		} else if (!is_null($isNew) && $isNew !="" ) {
			if ($isNew == 1) {
				$isNewStr = " having ( isNew > 0) ";
				$isNewCount = " and (select count(a.IsNew) from ad a where a.account = u.account and isNew='1' limit 1 )>0 ";
			} else {
				$isNewStr = " having ( isNew = 0) ";
				$isNewCount = " and (select count(a.IsNew) from ad a where a.account = u.account and isNew='1' limit 1 )=0 ";
			}
		}
		
		$countSql = "select count(u.account) from user u where (type =1 or type =2) $accountStr $nameStr $typeStr $isPayCount $isNewCount ";
		
		$count = @mysql_fetch_row(@mysql_query($countSql));
	 
		//(select count(s.count)  from settledetail s where s.account = u.account and s.chkSettle=1)  count,
		$sql = "select u.account, u.name, u.status, u.createTime, u.type, 
				u.count count,		
				(select count(a.IsNew) from ad a where a.account = u.account and isNew='1' limit 1 ) isNew
				from user u where (type =1 or type =2)  $accountStr $nameStr $typeStr  $isPayStr $isNewStr  limit $start , $limit";
				
		$result = @mysql_query($sql);
		$items = $this::toArray($result);
		$this->log->debug("count:".$countSql);
		$this->log->debug('data:'.$sql);
		return  array(
			"items" => $items,
			"count" => $count[0]
		);
 	
	}
	
	public function update($user) {
		
	}
	
	public function getTargetAccount() {
		$sql = "select account ,companyName  from user where type=1 and status=1";
		$result = @mysql_query($sql);
		return $this::toArray($result);
	}
	
	public function checkAccount($account) {
		$sql = "select count(*) from user where account= '$account'";
		$result =  @mysql_query($sql);
		$row = @mysql_fetch_row($result);
		return $row[0];
	}
	
	public function insert($user) {		
		$sql = $this->genInserSQL("user", $user);	 	
	 	$this->log->debug("新增使用者 :".$sql);
	 	$result = @mysql_query($sql);	

	 	//$notyfySql = "INSERT INTO adnotify (adId,content) values($adId, '<font color=red size=3>「您尚未刊登任何廣告，<a title=立即刊登廣告  href=/ad/html/frontoffice/tigerStep1.php>請立即使用</a>。」');";
			
			//mysql_query ( $notyfySql ) or die ( mysql_error () );
	 	//
	 	
	 	return $result;
	}
}
?>